//
//  HYUserButton.m
//  HaiYue
//
//  Created by skyline on 2018/1/8.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYUserFuncButton.h"
@interface HYUserFuncButton ()
@property (nonatomic, strong) HYImageView *IMG;
@property (nonatomic, strong) HYLabel *label;
@property (nonatomic, copy) NSString *normalImage;
@property (nonatomic, copy) NSString *selectImage;
@end
@implementation HYUserFuncButton
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title image:(NSString *)normalImage selectImage:(NSString *)selectImage
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.titleColor = TEXT_GRAY_COLOR;
        self.font = [UIFont systemFontOfSize:TipsFont weight:UIFontWeightLight];
        self.label.text = title;
        self.IMG.image = [UIImage imageNamed:normalImage];
        self.normalImage = normalImage;
        self.selectImage = selectImage;
    }
    return self;
}

- (HYImageView *)IMG
{
    if (!_IMG)
    {
        CGFloat radius  = (self.bounds.size.width - self.label.bounds.size.height - 5 ) / 2.0;
        _IMG = [[HYImageView alloc] initWithCornerRadius:radius];
        [self addSubview:_IMG];
        [_IMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self);
            make.size.mas_equalTo(self.IMG.bounds.size);
        }];
    }
    return _IMG;
}

- (HYLabel *)label
{
    if (!_label)
    {
        _label = [[HYLabel alloc] initWithText:@"" textColor:_titleColor textFont:_font];
        [self addSubview:_label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self);
        }];
    }
    return _label;
}

- (void)setTitleColor:(UIColor *)titleColor
{
    if (_titleColor != titleColor)
    {
        _titleColor = titleColor;
        _label.textColor = titleColor;
    }
}

- (void)setFont:(UIFont *)font
{
    if (_font != font)
    {
        _font = font;
        _label.font = font;
        [_label sizeToFit];
        [_label mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(self.label.bounds.size);
        }];
     
        CGFloat radius  = (self.bounds.size.width - _label.bounds.size.height - 5) / 2.0;
        _IMG.layer.cornerRadius = radius;
        [_IMG mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(radius * 2.0, radius * 2.0));
        }];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    self.IMG.image = [UIImage imageNamed:self.selectImage];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    self.IMG.image = [UIImage imageNamed:self.normalImage];

}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
